package com.bw.utils;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolUtil {
    public volatile static ThreadPoolExecutor ThreadPoolExecutor = null;

    private ThreadPoolUtil() {
    }

    // https://blog.csdn.net/weixin_44471490/article/details/108929289
    public static ThreadPoolExecutor getThreadPoolExecutor() {
        if (ThreadPoolExecutor == null) {
            synchronized (ThreadPoolUtil.class) {
                if (ThreadPoolExecutor == null) {
                    /**
                     * corePooLsize = 2 至少有2个线程
                     * MaximumPoolsize =10 最大有个10线程，当队列满5个new第3  4 5 直到10
                     * KeepAliveTime 线程存活的时间，假设有10个线程，任务降低，会归还8个
                     * Linked :队列
                     */
                    ThreadPoolExecutor = new ThreadPoolExecutor(
                            8, 16, 1, TimeUnit.MINUTES,
                            new LinkedBlockingDeque<>());
                }
            }
        }
        return ThreadPoolExecutor;
    }
}
